package com.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmer.pojo.Information;
import com.farmer.service.InformationService;
import com.farmer.mapper.InformationMapper;
import com.farmer.utils.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 86183
* @description 针对表【information(扶贫资讯表)】的数据库操作Service实现
* @createDate 2024-04-25 20:31:35
*/
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information>
    implements InformationService{

    @Autowired
 private InformationMapper informationMapper;

    @Override
    public Result add(Information information) {
        information.setTime(DateFormat.getDateInstance().format(new Date()));
        informationMapper.insert(information);
        return Result.ok(null);
    }
/*
* 单条删除*/
    @Override
    public Result delete(Integer id) {
        informationMapper.deleteById(id);
        return Result.ok(null);
    }

    /*
    * 批量删除*/
    @Override
    public Result deleteBatch(List<Integer> ids){
        for (Integer id :ids) {
            informationMapper.deleteById(id);
        }
        return Result.ok(null);
    }

    @Override
    public Result update( Information information) {
        informationMapper.updateById(information);
        return Result.ok(null);
    }


    @Override
    public Result getById(Integer id) {
        Information info = informationMapper.selectById(id);
        if (info != null) {
            return Result.build(info, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败");
        }
    }

    @Override
    public Result getAll() {
        List<Information> info = informationMapper.selectList(null);
        if (info != null && !info.isEmpty()) {
            return Result.build(info, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败");
        }
    }

    /*
    * 根据标题模糊查询*/
    @Override
    public Result getByTitleLike(String title) {
        // 构造查询条件
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", title); // 根据标题模糊查询
        // 执行查询
        List<Information> infos = informationMapper.selectList(queryWrapper);
        // 返回结果
        if (infos != null && !infos.isEmpty()) {
            return Result.build(infos, 111, "查询成功");
        } else {
            return Result.build(null, 101, "未找到匹配的信息");
        }
    }

    /*
* 显示前六条语句*/
    @Override
    public Result show() {
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit 6");
        List<Information> info = informationMapper.selectList(queryWrapper);
        return Result.ok(info);
    }

    /*
    * 分页查询*/
   @Override
    public Result page(Integer pageNum, Integer pageSize){
       // 创建分页构造对象
       Page<Information> page = new Page<>(pageNum, pageSize);
       // 执行分页查询
       IPage<Information> result = informationMapper.selectPage(page, null);
       // 封装到统一响应体Result中
       return Result.ok(result);
   }

}




